package cn.shenzz.suixiang.array.leetcode_209;

class Solution {
    public static void main(String[] args) {
        int[] nums = {2, 3, 1, 2, 4, 3};
        int target = 7;
        System.out.println(minSubArrayLen(target, nums));
    }

    public static int minSubArrayLen(int target, int[] nums) {
        int n = nums.length, sum = 0, res = Integer.MAX_VALUE, left = 0, right = 0;
        while (right < n) {
            sum += nums[right];
            while (sum >= target) {
                res = Math.min(res, right - left + 1);
                sum -= nums[left];
                left++;
            }
            ++right;
        }
        if (res == Integer.MAX_VALUE) return 0;
        else return res;
    }
}